{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import tensorflow as tf\n",
    "import transformer"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# tf.enable_eager_execution()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "vocab_size = 12\n",
    "hidden_size = 16\n",
    "filter_size = 16\n",
    "num_encoder_layers = 2\n",
    "num_decoder_layers = 2\n",
    "num_heads = 2\n",
    "label_smoothing = 0.1\n",
    "dropout = 0.1\n",
    "beam_size = 3\n",
    "model = transformer.TransformerEncoderDecoderModel(vocab_size, hidden_size,\n",
    "                                                   filter_size, num_heads,\n",
    "                                                   num_encoder_layers,\n",
    "                                                   num_decoder_layers,\n",
    "                                                   label_smoothing, dropout)\n",
    "\n",
    "X = tf.placeholder(tf.int64, (None, None))\n",
    "Y = tf.placeholder(tf.int64, (None, None))\n",
    "decoder = tf.placeholder(tf.int64, (None, None))\n",
    "\n",
    "# X = tf.ones((2, 7), tf.int64)\n",
    "# Y = tf.ones((2, 5), tf.int64)\n",
    "\n",
    "loss, outputs = model(\n",
    "    {\n",
    "        \"inputs\": X,\n",
    "        \"targets\": Y,\n",
    "        \"decoder\": decoder,\n",
    "    }, True)\n",
    "\n",
    "# outputs = model.predict(\n",
    "#   {\n",
    "#       \"inputs\": X,\n",
    "#   }, tf.shape(X)[1] + 10, 1, top_p = 0.8, temperature = 0.5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(<tf.Tensor 'softmax_cross_entropy_loss_1/value:0' shape=() dtype=float32>,\n",
       " {'logits': <tf.Tensor 'Reshape_13:0' shape=(?, ?, 12) dtype=float32>})"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "loss, outputs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "sess = tf.InteractiveSession()\n",
    "sess.run(tf.global_variables_initializer())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "l, o = sess.run([loss, outputs], feed_dict = {X: np.ones((2, 7)), Y: np.ones((2, 5)), decoder: np.ones((2, 5))})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0],\n",
       "       [0, 0, 0, 0, 0, 0, 0, 0, 7, 1, 0, 0, 0, 0, 0, 0, 0]])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "o['outputs']"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
